﻿using System;
using Framework.MVC;
using Log.Model;
using Billing.Model;
using Billing.IService.Entry;
using Pay.Entry;
using Framework.MVVM.Model;
using Log.IService;
using Log.Service;
using Common.IService;
using Common.Service;
using Billing.IService;
using Billing.Service;
using Billing.Model.Behavior;
using System.Collections.Generic;


namespace Pay.Models
{
    [ServiceAttribute(typeof(IPayOrderService), typeof(PayOrderService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    [ServiceAttribute(typeof(IBillingService), typeof(BillingService), "Instance")]
    public class PayOrderModel : AsyncModelBase, IWriteLog,INewPayOrder,IGetBilling,IModifyBilling
    {
        #region Field

        private const string m_error_neworder_amount = "newpay000";
        private const string m_error_neworder = "newpay001";
        private const string m_complete_neworder = "newpay002";
        private const string m_info_neworder = "newpay003";
        private const string m_empty_billing = "newpay004";
        private const string m_error_getbilling = "newpay005";
        private const string m_complete_getbilling = "newpay006";
        private const string m_error_modifybilling_amount = "newpay007";
        private const string m_error_modifybilling = "newpay008";
        private const string m_complete_modifybilling = "newpay009";
        private const string m_complete_payorder = "newpay010";

        private const string m_entry = "entry";

        private const string m_split = ";";

        private PayCenterOrderEntry m_order;

        #endregion Field

        #region Property

        public string Source { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.NewPayOrder(m_complete_neworder, m_error_neworder);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region INewPayOrder

        public void OnNewPayOrderComplete(string key, PayOrderEntry entry)
        {
            OnBeginRequest();
            this.Log(m_info_neworder, Source + m_split + entry.code);
            OnBeginRequest();
            this.Log(key, entry);
            m_order = new PayCenterOrderEntry {
                code = entry.code,
                amount = entry.amount,
                mode = entry.sourceCode,
                orderCode = entry.orderCode,
                owner = entry.owner
            };

            BillingCode = entry.owner;
            BillingSource = entry.sourceCode;
            this.GetBilling(m_error_getbilling, m_complete_getbilling);
        }

        public void OnNewPayOrderError(string key)
        {
            this.Log(key, Source);
        }

        public int PayOrderAmount { get; set; }

        public string PayOrderCode { get; set; }

        public string PayOrderOwner { get; set; }

        public string PayOrderSource { get; set; }

        #endregion INewPayOrder

        #region IGetBilling

        public string BillingCode { get; set; }

        public string BillingSource { get; set; }

        public void OnGetBillingComplete(string key, BillingEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            BillingAmount = entry.amount - PayOrderAmount;
            BillingVersion = entry.version;

            this.ModifyBilling( m_error_modifybilling, m_complete_modifybilling);
        }

        public void OnGetBillingError(string key)
        {
            this.Log(key, Source + m_split + BillingCode);
        }

        #endregion IGetBilling

        #region IModifyBilling

        public decimal BillingAmount { get; set; }

        public byte BillingVersion { get; set; }

        public void OnModifyBillingComplete(string key, BillingEntry entry)
        {
            OnComplete(m_entry,m_order);
            OnBeginRequest();
            this.Log(key, entry);
            this.Log(m_complete_payorder, m_order);
            
        }

        public void OnModifyBillingError(string key)
        {
            this.Log(key, new BillingEntry { code = BillingCode, amount = BillingAmount, version = BillingVersion, sourceCode = PayOrderSource });
        }

        #endregion IModifyBilling
    } 
}